﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;


namespace SSP
{
    class SSPNode
    {
        public Vector2 position;
        public Vector2 anchorPoint;
        public Vector2 absolutePosition;
        public Vector2 scale;
        public float scaleX;
        public float scaleY;
        public float rotation;
        public int zOrder;
        public int tag;
        public bool visible;
        public ArrayList children;
        public SSPNode parent;
        public bool isScene;

        public SSPGame gameInstance;

        public SSPNode()
        {
            position = Vector2.Zero;
            anchorPoint = Vector2.Zero;
            absolutePosition = Vector2.Zero;
            scale = new Vector2(1, 1);
            scaleX = 1.0f;
            scaleY = 1.0f;
            rotation = 0.0f;
            zOrder = 0;
            visible = true;
            children = new ArrayList();
            parent = null;
        }

        public SSPNode(SSPGame g)
        {
            gameInstance = g;
            position = Vector2.Zero;
            anchorPoint = Vector2.Zero;
            absolutePosition = Vector2.Zero;
            scaleX = 1.0f;
            scaleY = 1.0f;
            rotation = 0.0f;
            zOrder = 0;
            visible = true;
            children = new ArrayList();
            parent = null;
        }



        public void addChild(SSPNode node)
        {
            if (!children.Contains(node))
            {
                node.zOrder = 0;
                node.parent = this;
                children.Add(node);
                //Console.WriteLine("Add Child =  {0} ", children.Count);
               // Console.WriteLine("# - {0}", this);
               // foreach (SSPNode n in children)
               // {
               //     Console.WriteLine("## - {0}", n);
               // }
            }
        }

        public void addChild(SSPNode node, int z)
        {
            if (!children.Contains(node))
            {
                node.zOrder = z;
                node.parent = this;
                children.Add(node);
            }
        }

        public void addChild(SSPNode node, int z, int t)
        {
            if (!children.Contains(node))
            {
                node.tag = t;
                node.zOrder = z;
                node.parent = this;
                children.Add(node);
            }
        }

        public virtual void draw()
        {
        }

        public void init()
        {
        }

        public void onEnter()
        {
        }

        public void onExit()
        {
        }

        public void removeAllChild()
        {
            foreach (SSPNode n in children)
            {
                n.tag = 0;
                n.zOrder = 0;
            }

            children.Clear();
        }

        public void removeChild(SSPNode node)
        {
            if (children.Contains(node))
            {
                children.Remove(node);
            }
        }

        public void removeChildByTag(int t)
        {
            SSPNode temp = null;

            foreach (SSPNode n in children)
            {
                if (n.tag == t)
                {
                    temp = n;
                }
                else
                {
                    temp = null;
                }
            }

            if (temp != null)
            {
                children.Remove(temp);
            }
        }

        public void removeFromParent()
        {
            if (parent != null)
            {
                parent.removeChild(this);
            }
        }

        public void reorderChild(SSPNode child, int z)
        {
            foreach (SSPNode n in children)
            {
                if (n == child)
                {
                    n.zOrder = z;
                }
            }
        }

        public virtual void update()
        {
            //Console.WriteLine("Node Update()");
            scale = new Vector2(scaleX, scaleY);
            if (parent != null)
            {
                if (parent.parent == null)
                {
                    absolutePosition = position;
                }
            }
            foreach (SSPNode n in children)
            {
                n.absolutePosition = Vector2.Add(n.position, this.position);
                n.update();
            }
             
        }
    }
}
